A new method to compute the last nonzero digit of n!
====================================================

    Substituting p for 5 in the congruence found in exercise 40, section 4 of
the book Concrete Mathematics, [1], we get

n!/(5^M) # (-l)^M * a_N!...a_1!a_0! (mod 5)                             (1)

where M is the multiplicity of 5 in the factorization of n! , and
      (a_N ... a_1 a_0)5 is the radix 5 representation of n.

Representing the left size of (1) by the product a.x, and the right term by
P, we have,

a.x # P (mod 5)                                                         (2)

    and can make

a = 2^M mod 5;                                                          (3)

x = N!/( 5^M * 2^M ); and                                               (4)

P = (-l)^M * (a_N!...a_1!a_0! mod 5).                                   (5)

    We can determine P making Q := (a_N!...a_1!a_0! mod 5); if M is even P is
equal to Q; on the contrary P is -Q + 5.

    The product Q is equal to 2^t mod 5, where t is the sum of half of each
even a_i , 0 <= i <= N. This is true since 0! = 1! = 1, 2! = 2, 3! mod 5 = 1,
and 4! mod 5 = 4 = 2^2; So we have a product with only twos and fours, i.e.,
the terms of Q are only 2^1, or 2^2, and we add respectively 1 and 2 to t.

    Because P is always positive, there are only 16 possible values of the
parameters a, P of (2). This follows from the fact that the possible values of
(3) and (5) are 1,2,3, or 4 because 2^M5 is not divisible by 5, and P is not
zero. So we can solve the following versions of (2) to find the last nonzero
digit of n! u, u = x mod 10. (Below the module 5 is implicit.)

a P
1 1  x # 1 => u = 1+5 = 6.
  2  x # 2 => u = 2.
  3  x # 3 => x # 8 => u = 8.
  4  x # 4 => u = 4.
2 1  2x # 1, 2x # 6, x # 3, u = 8.
  2  2x # 2, x # 1, u = 6.
  3  2x # 3, 2x # 8, x # 4, u = 4.
  4  2x # 4, x # 2, u = 2.
3 1  3x # 1, 3x # 6, x # 2, u = 2.
  2  3x # 2, 3x # -3, u = 4.
  3  3x # 3, x # 1, u = 6.
  4  3x # 4, 3x # 9, x # 3, u = 8.
4 1  4x # 1, 4x #-4, x #-1, u = 4.
  2  4x # 2. Using LAW B of [2] section 1.2.4 we can multiply the terms
             respectively by (-1) and 4. So -4x # 8, x # -2, and u = 8.
  3  4x # 3, 4x # 8, x # 2, u = 2.
  4   x # 1, u = 6.

    The values of u in the order that they appear above form the sequence,

6, 2, 8, 4, 8, 6, 4, 2, 2, 4, 6, 8, 4, 8, 2, 6

with offset 0, we can index its terms with z := 4(a-1) + P-1. So we have

z: 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
   6, 2, 8, 4, 8, 6, 4, 2, 2, 4, 6, 8, 4, 8, 2, 6

the sequence is symmetric

6, 2, 8, 4, 8, 6, 4, 2, <-> 2, 4, 6, 8, 4, 8, 2, 6

so we can make

z := z*(1-[z/8]) + (15-z)*[z/8] to index all the terms using indices 0 to 7.

z: 0  1  2  3  4  5  6  7
   6, 2, 8, 4, 8, 6, 4, 2
   ----V----   ----V----
       |           L> the points (4,8),...(7,2) are on the line f2(z) = 16 - 2z.
       |
       L> the points (0,6),...,(3,4) belong to f1(z) = 6+ (45z^2 -10z^3 -47z)/3,

so u is equal to f1(z) if z < 4. Otherwise u is given by f2(z).

    We conclude that for n >= 5, the value of u calculated by the formula below
is the last nonzero digit of n!.

M = \sum_{i=1}^[log_5(n)] [n/5^i] ,
t = \sum_{i=0}^N (1 - a_i % 2) * a_i/2 ,
a := (2^M) mod 5;  Q := (2^t) mod 5;
if M is even let P = Q else P := -P + 5;
z := 4(a-1) + P-1; z := z(1- [z/8]) + [z/8](15-z);
if z is less than 4, u := 6 + (45z^2 -10z^3 -47z)/3; else u := 16 - 2z;

    Both the sums can be computed together in a single program loop. For
example we have the PARI code

Amax = floor(log(n)/log(5)); M=0; t=0;
for(a=1, Amax,
    aux = n;
    n = floor(n/5);
    M += n;
    r = aux -5*n;
    t += (1 - r%2) * r/2;
    );
t += (1 -n%2) * n/2;
----------------------------------------------------------------------

References
[1] http://www.xpmath.com/ebooks/files/concrete.pdf
[2] D. E. Knuth, The Art of Computer Programming, vol.1.

-----------------------------------------------------------------------------------

Appendix
========

Lemma
    If x mod 5 = k, x mod 10 = k, or k+5.
x = 5[x/5] + k = 5[2a/10] + k = 10[x/10] + r                (1)
where r = x mod 10.                                         (2)

From (1) r = 5[2a/10] - 10[x/10] + k.                       (3)

Let u be the last digit of x.
If u < 5, [2a/10] = 2[x/10] ,                               (4)
and If u >= 5, [2a/10] = 2[x/10] + 1.                       (5)

    The identities (4) and (5) follows from the value of the first carry when
we multiply 2 by x. If u < 5 this carry is 0. Otherwise it is 1. For example
consider x = 32; 2a = 64 and (4) gives 6 = 6. On the other hand, if x = 46,
2a = 92; (5) gives 9 = 9.

From (3) and (4) r = 5.2[x/10] - 10[x/10] + k = k.
From (3) and (5) r = 5.(2[x/10]+1) - 10[x/10] + k = k + 5.
